﻿using Basic.FlowCollect;
using Basic.FlowRemoteModel;
using WeDonekRpc.Client.Interface;

namespace Basic.FlowApplication.AuditLocalEvent.AuditEvent
{
    internal class AppAuditState : IEventHandler<FlowAuditEvent>
    {
        private readonly IFlowAppItemCollect _AppItem;

        public AppAuditState ( IFlowAppItemCollect appItem )
        {
            this._AppItem = appItem;
        }

        public void HandleEvent ( FlowAuditEvent data, string eventName )
        {
            if ( data.Log.AppType == FlowAppType.外部应用 )
            {
                return;
            }
            else if ( data.Log.Status == FlowAuditLogStatus.已结束 )
            {
                this._AppItem.SetStatus(data.Log.LinkPk, this._GetStatus(data.Log.AuditStatus), data.Log.EndTime);
            }
            else if ( data.Item.NodeType == FlowNodeType.开始节点 && data.Item.AuditState == FlowAuditItemStatus.审核通过 && !data.Item.PrevNodeId.HasValue )
            {
                this._AppItem.SetStatus(data.Log.LinkPk, FlowItemAuditStatus.审核进行中, null);
            }
        }
        private FlowItemAuditStatus _GetStatus ( FlowAuditStatus status )
        {
            switch ( status )
            {
                case FlowAuditStatus.已关闭:
                    return FlowItemAuditStatus.已取消;
                case FlowAuditStatus.已取消:
                    return FlowItemAuditStatus.已取消;
                case FlowAuditStatus.审核通过:
                    return FlowItemAuditStatus.审核通过;
                case FlowAuditStatus.审核未通过:
                    return FlowItemAuditStatus.审核未通过;
                case FlowAuditStatus.已作废:
                    return FlowItemAuditStatus.已取消;
                default:
                    return FlowItemAuditStatus.审核进行中;
            }
        }
    }
}
